﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using WP8DataModel.Model;

namespace WP8DataModel.ViewModel
{
    public class PlayerAgentViewModel : INotifyPropertyChanged
    {
        public static readonly string DefaultDbConnectionString = "Data Source=isostore:/PlayerAgentInfo.sdf";
        private readonly PlayerAgentDataContext _db;

        public PlayerAgentViewModel(string iConnectionString)
        {
            _db = new PlayerAgentDataContext(iConnectionString);
        }

        private ObservableCollection<StreamDescription> _allStreamDescriptions;
        public ObservableCollection<StreamDescription> AllStreamDescriptions
        {
            get { return _allStreamDescriptions; }
            set
            {
                _allStreamDescriptions = value;
                NotifyPropertyChanged("AllStreamDescriptions");
            }
        }

               // Query database and load the collections and list used by the pivot pages.
        public void LoadCollectionsFromDatabase()
        {

            // Specify the query for all to-do items in the database.
            var aStreamsInDb = from StreamDescription todo in _db.Streams
                select todo;

            // Query the database and load all to-do items.
            AllStreamDescriptions = new ObservableCollection<StreamDescription>(aStreamsInDb);
        }

        // Write changes in the data context to the database.
        public void SaveChangesToDb()
        {
            _db.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
